package com.imstlife.vending.sgy.Utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;


import com.imstlife.vending.sgy.logger.LocalLog;

import java.util.Calendar;
import java.util.Random;


/**
 * Created by shengguangyu on 2019/3/19.
 * personal email:270047276@qq.com
 */
public class ImstUtils {
    private static final String TAG ="ImstUtils" ;

    /**
     * 设置定时重启
     */
    public static void initAlarm(Handler handler, int what) {
        int hourTime;
        int minuteTime;
        Random ra =new Random();
        int rNum = ra.nextInt(120)+1;
        if (rNum==0){
            hourTime = 2;
            minuteTime = 0;
        }else if (rNum == 60){
            hourTime = 3;
            minuteTime = 0;
        }else if (rNum == 120){
            hourTime = 4;
            minuteTime = 0;
        }else{
            if (rNum>60){
                hourTime = 3;
                minuteTime = rNum - 60;
            }else{
                hourTime = 2;
                minuteTime = rNum;
            }
        }
        LocalLog.e("reDeviceTime",hourTime +"  "+minuteTime);
        Calendar c = Calendar.getInstance();// 获取日期对象
        c.setTimeInMillis(System.currentTimeMillis()); // 设置Calendar对象
        c.set(Calendar.HOUR_OF_DAY, hourTime); // 设置目标小时数
        c.set(Calendar.MINUTE, minuteTime); // 设置目标的分钟数
        c.set(Calendar.SECOND, 0); // 设置目标的秒数
        c.set(Calendar.MILLISECOND, 0); // 设置目标的毫秒数

        long diffTime = c.getTimeInMillis() - System.currentTimeMillis();
        if (diffTime < 0)
            diffTime = diffTime + 24 * 60 * 60 * 1000;
        if (diffTime > 0) {
            if (handler.hasMessages(what))
                handler.removeMessages(what);
            handler.sendEmptyMessageDelayed(what, diffTime);
            LocalLog.d(TAG, "设备将在" + diffTime + "毫秒后重启");
        }

    }

    /**
     * 设置定时广播
     *
     * @param context
     * @param actionHour   执行时间，3代表凌晨3点
     * @param actionIntent
     */
    public static void initAlarm(Context context, int actionHour, String actionIntent) {
        int interval = 24 * 60 * 60 * 1000;
        Calendar c = Calendar.getInstance();// 获取日期对象
        c.setTimeInMillis(System.currentTimeMillis()); // 设置Calendar对象
        c.set(Calendar.HOUR_OF_DAY, actionHour); // 设置闹钟小时数
        c.set(Calendar.MINUTE, 0); // 设置闹钟的分钟数
        c.set(Calendar.SECOND, 0); // 设置闹钟的秒数
        c.set(Calendar.MILLISECOND, 0); // 设置闹钟的毫秒数
        Intent intent = new Intent(actionIntent); // 创建Intent对象

        //        if (App.currentapiVersion>19){
        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        mgr.cancel(pi);
        if (c.getTimeInMillis() <= System.currentTimeMillis()) {
            mgr.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() + interval, pi);
        } else {
            mgr.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
        }

        //        }else{
        //            PendingIntent pi = PendingIntent.getBroadcast(context, 0,
        //                    intent, 0); // 创建PendingIntent
        //            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        //            if (c.getTimeInMillis() <= System.currentTimeMillis())
        //                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        //                        c.getTimeInMillis() + interval,
        //                        interval, pi);
        //            else
        //                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        //                        c.getTimeInMillis(), interval, pi);
        //
        //        }
    }

    public static String getVersionName(Context context) {
        String versionName = "1.0";
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static int getVersionCode(Context context) {
        int versionCode = 1;
        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }
}
